package com.edu.test;

import com.edu.bean.Student;
import com.edu.bean.User;
import com.edu.mapper.StudentMapper;
import com.edu.mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

public class MyTest2 {

    SqlSession sqlSession = null;

    @Before
    public void init() throws IOException {
        String path = "mybatis-config.xml";//路径
        InputStream inputStream = Resources.getResourceAsStream(path);
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        sqlSession = sessionFactory.openSession();
    }

    /*
    *   当参数为一个简单类型时
    **/
    @Test
    public void test01(){
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
        System.out.println(studentMapper.selectStudentById(1));
    }


    @Test
    public void test02(){
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
        Student student = new Student();
        student.setStuName("翠花");
        student.setPassword("12456");
        student.setStuSex("女");
        student.setStuAge(20);
        studentMapper.insertStudent(student);
    }


    @Test
    public void test03(){
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
        studentMapper.selectStudent2("张三","男");
    }


    @Test
    public void test04(){
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
        Student stu = new Student();
        stu.setStuSex("女");
        studentMapper.selectStudent3(stu,15,25);
    }

    @Test
    public void test05(){
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
        int[] ages = {15,25,22};
        studentMapper.selectStudent4(ages);
    }


    @Test
    public void test06(){
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
        List<Integer> ages = new ArrayList<>();
        ages.add(15);
        ages.add(25);
        ages.add(22);
        studentMapper.selectStudent5(ages);
    }

    @Test
    public void test07(){
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);

        Map<String,Object> map = new HashMap<>();
        map.put("sex","男");
        map.put("min",15);
        map.put("max",25);
        studentMapper.selectStudent6(map);
    }

}
